package com.blue.waffle.iwc.screens;

import java.sql.SQLException;
import java.util.List;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.blue.waffle.iwc.domains.Difficulty;
import com.blue.waffle.iwc.domains.Level;
import com.blue.waffle.iwc.game.IWCGame;
import com.blue.waffle.iwc.ui.ButtonActorListener;
import com.blue.waffle.iwc.ui.ParallaxBackground;
import com.blue.waffle.iwc.ui.ParallaxLayer;

public class DifficultyScreen extends AbstractScreen {

	private static final int BUTTON_WIDTH=300;
	private static final int BUTTON_HEIGHT=40;
	private List<Difficulty> difficulties;
	
	public DifficultyScreen(IWCGame game) {
		super(game);		
	}
	
	@Override
	public void show() {
		super.show();
		
		Skin skin = getUiSkin();
		Table table = new Table(skin);
		Table table2 = new Table(skin);
		table.setFillParent(true);
		table2.setFillParent(true);
		table2.right().bottom();
		
		gameStage.addActor(table);		
		gameStage.addActor(table2);
		
		try {
			difficulties=game.getDatabaseHelper().getDifficultyHelper().selectAll();
			
			for (Difficulty difficulty : difficulties) {
				
				TextButton GameButton = new TextButton(difficulty.getName(), skin);
				GameButton.addListener(new ButtonActorListener(this,difficulty){
					@Override
					public void clicked(InputEvent event, float x, float y) {
						super.clicked(event, x, y);
						Difficulty difficulty=(Difficulty)getData();
						try {
							game.getGameManager().setCurrentDifficulty(difficulty).PersistProfile();
							//List<Level> levels=game.getDatabaseHelper().getLevelHelper().selectAll(difficulty, 2);
							System.out.println(game.getGameManager().getUserProfile().getId());
							System.out.println(game.getGameManager().getUserProfile().getLastPlayedLevel());
							System.out.println(game.getGameManager().getUserProfile().getCurrentDifficulty().getId());
							System.out.println(game.getGameManager().getUserProfile().getCurrentDifficulty().getName());
						} catch (SQLException e) {
							e.printStackTrace();
						}
						
						screen.game.setScreen(new GameScreen(screen.game, difficulty));
						
					}
				});
				table.add(GameButton).size(BUTTON_WIDTH,BUTTON_HEIGHT).uniform().spaceBottom(10);		
				table.row();
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		gameStage.addActor(getButtonTable(skin,new ButtonActorListener(this){
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				game.setScreen(new MenuScreen(game));
			}
		}).right().bottom());
		
		
		
		/*TextButton mediumButton = new TextButton("Medium", skin);
		mediumButton.addListener(new DifficultyActorListener(this, Button.Medium));
		table.add(mediumButton).size(BUTTON_WIDTH,BUTTON_HEIGHT).uniform().spaceBottom(10);
		table.row();
		
		TextButton hardButton = new TextButton("Hard", skin);
		hardButton.addListener(new DifficultyActorListener(this,Button.Hard));
		table.add(hardButton).size(BUTTON_WIDTH,BUTTON_HEIGHT).uniform().spaceBottom(10);
		table.row();
		
		TextButton backButton = new TextButton("Back", skin);
		backButton.addListener(new DifficultyActorListener(this, Button.Back));
		table2.add(backButton).size(200, 40).center();
		table2.row();*/
	}
	
	/*public static class DifficultyActorListener extends ButtonActorListener{
		public enum Button{
			Easy("easy"),
			Normal("normal"),
			Hard("hard"),
			Back("back");
			
			private String title;
			
			Button(String title){
				this.title=title.toLowerCase();
			}
			
			public String getTitle() {
				return title;
			}
			
		};
		private Button type;
		
		public DifficultyActorListener(DifficultyScreen screen, Button type){
			super(screen);
			this.type = type;
		}
		
		@Override
		public void clicked(InputEvent event, float x, float y) {
			super.clicked(event, x, y);
			switch(type) {
			case Easy:
				System.out.println("easy game");
				break;
			case Normal:
				System.out.println("medium game");
				break;
			case Hard:
				System.out.println("hard game");
				break;
			case Back:
				screen.getGame().setScreen(new MenuScreen(screen.getGame()));
			default:
				break;					
			}
		}
		
	}*/
}
